package com.mmall.controller.backend;

import com.mmall.common.ServerResponse;
import com.mmall.service.IOrderService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/manage/order")
public class OrderManageController {

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IUserService iUserService;

    /**订单列表
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public ServerResponse orderList(HttpServletRequest request,
                                    @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isEmpty(loginToken)) {
//            return ServerResponse.retErrorMsg("用户未登录，无法获取信息");
//        }
//        String userJsonStr = RedisPoolUtil.get(loginToken);
//        User user = JsonUtil.stringToObj(userJsonStr, User.class);
//        if (user == null) {
//            return ServerResponse.retErrorCode(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()) {
//            //订单列表
//            return iOrderService.manageList(pageNum,pageSize);
//        } else {
//            return ServerResponse.retErrorMsg("无权限操作,需要管理员权限");
//        }
        //全部通过拦截器验证是否登陆以及权限
        return iOrderService.manageList(pageNum,pageSize);
    }

    /**管理员订单详情
     * @param request
     * @param orderNo
     * @return
     */
    @RequestMapping("detail")
    @ResponseBody
    public ServerResponse orderDetail(HttpServletRequest request, Long orderNo) {
        //全部通过拦截器验证是否登陆以及权限
        return iOrderService.manageDetail(orderNo);
    }

    @RequestMapping("search")
    @ResponseBody
    public ServerResponse orderSearch(HttpServletRequest request, Long orderNo,
                                      @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        //全部通过拦截器验证是否登陆以及权限
        return iOrderService.manageSearch(orderNo,pageNum,pageSize);
    }

    /**发货状态更改
     * @param request
     * @param orderNo
     * @return
     */
    @RequestMapping("sendGoods")
    @ResponseBody
    public ServerResponse orderSendGoods(HttpServletRequest request, Long orderNo) {
        //全部通过拦截器验证是否登陆以及权限
        return iOrderService.manageSendGoods(orderNo);
    }
}
